/**
 * 
 */
package com.cea.papyrus.diagram.component.editpart;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.XYLayout;
import org.eclipse.gef.EditPolicy;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Port;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.ContainerHighlightEditPolicy;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * @author AL090761
 *
 */

public class PackageEditPart extends
		com.cea.papyrus.diagram.classdiagram.editpart.PackageEditPart {

	/**
	 * 
	 */
	public PackageEditPart(GraphNode graphNode,ModelManager modelManager) {
		// TODO Auto-generated constructor stub
		super(graphNode,modelManager);
	}
	/**
	 * 
	 */
	@Override
	protected void createEditPolicies(){
		super.createEditPolicies();

		// edit poilicies are installed only if presentation is not shape
		if(getPresentation() != ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION) {

			installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new ContainerHighlightEditPolicy());//Does not run?!!
			installEditPolicy(EditPolicy.LAYOUT_ROLE, new ComponentLayoutEditPolicy(
				(XYLayout ) getContentPane().getLayoutManager()));
		}

		// Install mouse hover
		installActionHandle();
		// Add shortcuts on package figure for creating a new owned package
	}
	@Override
	protected List getModelChildren() {
		ArrayList contained= new ArrayList();
		ArrayList ports= new ArrayList();
		ArrayList labels=new ArrayList();

		// if the presentation of the element is not shape we can display children, otherwise we can't
		if(getGraphNode().getSemanticModel() != null) {
			if((getGraphNode().getSemanticModel().getPresentation()==null) ||
					!(getGraphNode().getSemanticModel().getPresentation().equals(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION))) {
				Iterator it=getGraphNode().getContained().iterator();
				while (it.hasNext()){
					GraphElement ge=(GraphElement)it.next();
					Element ue = null; 
					
					if (!(ge instanceof GraphEdge) ) {
						if ( ((GraphNode)ge).getSemanticModel() != null ) {			    		
							if (((Uml1SemanticModelBridge) ((GraphNode)ge).getSemanticModel()).getElement() != null) {
								ue = ((Uml1SemanticModelBridge) ((GraphNode)ge).getSemanticModel()).getElement();
								if (ue instanceof  Port) {
									ports.add(ge);        // Ports will be added later to be displayed on top
								}
								else {
									contained.add(ge);
								}
							} else { 
								contained.add(ge);
							}					    	
						} else {  // can be a comment link for instance
							contained.add(ge);
						}
					}
					// if it is a graphEdge there is nothing to do
				}
			}
		}
		// Now we can add ports	and labels
//		contained.addAll(labels);
		contained.addAll(ports);

		return contained;
	}
}
